;(load "compress.lsp")
;compress 重写一遍以复习
(defun compress (lst)
	(if (consp lst)
		(compr (car lst) 1 (cdr lst))
		lst))

(defun compr (e n lst)
	(if (null lst)
		(list (n-elts e n))
		(if (eql e (car lst))
			(compr e (+ n 1) (cdr lst))
			(cons (n-elts e n) (compr (car lst) 1 (cdr lst))))))

(defun n-elts (e n)
	(if (> n 1)
		(list n e)
		e))

(setf a '(1 1 1 0 1 0 0 0 0 1))
(format t "~A compressed is:~%~A~%" a (compress a))

;uncompress
(defun uncompress (lst)
	(if (null lst)
		nil
		(let ((e (car lst))
					(r (uncompress (cdr lst))))
			(if (consp e)
				(append (apply #'list-of e) r)
				(cons e r)))))

(defun list-of (n e)
	(if (zerop n)
		nil
		(cons e (list-of (- n 1) e))))

(format t "~A uncompress is:~%~A~%" (compress a) (uncompress (compress a)))

; my version
(defun unzip (lst)
	(if (null lst)
		nil
		(if (consp (car lst))
			(append (my-list-of (car lst)) (unzip (cdr lst)))
			(cons (car lst) (unzip (cdr lst))))))

(defun my-list-of (nlst)
	(let ((lst nil) (e (car (cdr nlst))) (n (car nlst)))
		(do ((i 1 (+ i 1))) 
			((> i n) lst)
			(progn
				;(format t "lst : ~A, i: ~A~%" lst i)
				(setf lst (cons e lst))))))


(format t "~A unzip is:~%~A~%" (compress a) (unzip (compress a)))
;(format t "~A my-list-of is:~A~%" '(5 1) (my-list-of '(5 1)))



